package com.fjwt.gz.mch.ctrl.proxy;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.core.model.security.GzUserDetails;
import com.fjwt.gz.db.dto.QrcodeRecordDTO;
import com.fjwt.gz.db.entity.QrcodeRecordEntity;
import com.fjwt.gz.db.vo.QrcodeRecordVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.QrcodeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

/**
 * 生成卡券二维码记录
 */
@Slf4j
@RestController
@RequestMapping("/api/proxy/qrcodeRecord")
public class QrcodeRecordProxyController extends CommonCtrl {

    @Autowired
    private QrcodeRecordService qrcodeRecordService;

    /**
     * 新增二维码记录
     */
    @PostMapping("/saveRecord")
    public ApiRes saveRecord(){
        QrcodeRecordEntity object = getObject(QrcodeRecordEntity.class);
        if(null == object.getActivityId()){
            return ApiRes.customFail("网络异常");
        }
        log.info("生成卡券二维码接口调用：请求地址：/api/qrcodeRecord/saveRecord，请求参数：{}", JSON.toJSONString(object));
        object.setCreatedAt(new Date());
        GzUserDetails currentUser = getCurrentUser();
        object.setCreatedUid(currentUser.getSysUserId());
        int insert = qrcodeRecordService.getBaseMapper().insert(object);
        if(insert>0){
            return ApiRes.ok(object.getQrcodeRecordId());
        }
        return ApiRes.customFail("操作失败！");
    }

    /**
     * 生成二维码、或者查看二维码
     * @return
     */
    @PostMapping("/getUnlimitedQRCode")
    public ApiRes getUnlimitedQRCode(){
        Long qrcodeRecordId = getValLong("qrcodeRecordId");
        log.info("生成查看二维码接口调用：请求地址：/api/qrcodeRecord/saveRecord，二维码记录id：{}",qrcodeRecordId);
        if(Objects.isNull(qrcodeRecordId)){
            return ApiRes.customFail("网络故障，请稍后重试！");
        }
        String code = qrcodeRecordService.getUnlimitedQRCode(qrcodeRecordId);
        return ApiRes.ok(code);
    }

    /**
     * 获取领券二维码记录
     * @return
     */
    @PostMapping("/getQrcodeRecordList")
    public ApiRes getQrcodeRecordList(){
        QrcodeRecordDTO qrcodeRecordDTO = getObject(QrcodeRecordDTO.class);
        String appid = request.getHeader("appid");
        qrcodeRecordDTO.setAppId(appid);
        log.info("获取领券二维码记录接口调用：请求地址：/api/qrcodeRecord/getQrcodeRecordList，参数：{}", JSONUtil.toJsonStr(qrcodeRecordDTO));
        IPage<QrcodeRecordVO> list = qrcodeRecordService.getAppQrcodeRecordList(getIPage(), qrcodeRecordDTO);
        return ApiRes.page(list);
    }

    /**
     * 获取二维码详情
     * @return
     */
    @PostMapping("/getRecordDetail")
    public ApiRes getRecordDetail(){
        Long qrcodeRecordId = getValLong("qrcodeRecordId");
        log.info("获取二维码详情接口调用：请求路径：/api/qrcodeRecord/getRecordDetail，二维码记录id：{}",qrcodeRecordId);
        if(Objects.isNull(qrcodeRecordId)){
            return ApiRes.customFail("网络故障，请稍后重试！");
        }
        QrcodeRecordVO detail = qrcodeRecordService.getRecordDetail(qrcodeRecordId);
        QrcodeRecordVO dto = new QrcodeRecordVO();
        BeanUtils.copyProperties(detail,dto);
        return ApiRes.ok(dto);
    }

}
